﻿<template>
  <a-modal
    title="编辑库位信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            disabled
            placeholder="请输入编码"
            v-decorator="['placecode', { rules: [{ required: true, message: '请输入编码！' }] }]"
          />
        </a-form-item>
        <a-form-item label="所在库区" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-select
            style="width: 100%"
            placeholder="请选择所在库区"
            v-decorator="['areaid', { rules: [{ required: true, message: '请选择所在库区！' }] }]"
          >
            <a-select-option v-for="(item, index) in WmsAreaData" :key="index" :value="item.code">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="排" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入排" style="width: 100%" v-decorator="['rowno']" :min="0" />
        </a-form-item>
        <a-form-item label="列" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入列" style="width: 100%" v-decorator="['columnno']" :min="0" />
        </a-form-item>
        <a-form-item label="层" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入层" style="width: 100%" v-decorator="['layerno']" :min="0" />
        </a-form-item>
        <a-form-item label="巷道" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number
            placeholder="请输入巷道"
            style="width: 100%"
            v-decorator="['aisle', { rules: [{ required: true, message: '请输入巷道！' }] }]"
            :min="0"
          />
        </a-form-item>
        <a-form-item label="进深号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入进深号" style="width: 100%" v-decorator="['deepcellno']" :min="0" />
        </a-form-item>
        <a-form-item label="线号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入线号" style="width: 100%" v-decorator="['line']" :min="0" />
        </a-form-item>
        <a-form-item label="堆垛机内部位置" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入堆垛机内部位置" v-decorator="['positionnoForSrm']" />
        </a-form-item>
        <a-form-item label="库位X坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入库位X坐标" style="width: 100%" v-decorator="['xzb']" :min="0" />
        </a-form-item>
        <a-form-item label="库位Y坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入库位Y坐标" style="width: 100%" v-decorator="['yzb']" :min="0" />
        </a-form-item>
        <a-form-item label="库位Z坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入库位Z坐标" style="width: 100%" v-decorator="['zzb']" :min="0" />
        </a-form-item>
        <a-form-item label="库位长度" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入库位长度" style="width: 100%" v-decorator="['length']" :min="0" />
        </a-form-item>
        <a-form-item label="库位宽度" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入库位宽度" style="width: 100%" v-decorator="['width']" :min="0" />
        </a-form-item>
        <a-form-item label="库位高度" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入库位高度" style="width: 100%" v-decorator="['height']" :min="0" />
        </a-form-item>
        <a-form-item label="最大承重" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入最大承重" style="width: 100%" v-decorator="['maxweight']" :min="0" />
        </a-form-item>
        <a-form-item label="库位高度级别" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-select style="width: 100%" placeholder="请选择库位高度级别" v-decorator="['heightlevel']">
            <a-select-option v-for="(item, index) in heightlevelData" :key="index" :value="item.code">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="优先级" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入优先级" style="width: 100%" v-decorator="['priority']" :min="0" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { WmsPlaceFkWmsAreaList, WmsPlaceEdit } from '@/api/modular/main/WmsPlaceManage'
export default {
  data() {
    return {
      Id: 0,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      record: {},
      WmsAreaData: [],
      heightlevelData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    WmsPlaceFkWmsAreaList() {
      WmsPlaceFkWmsAreaList().then((res) => {
        this.WmsAreaData = res.data
      })
    },
    // 初始化方法
    edit(record) {
      this.visible = true
      this.Id = record.id
      this.$nextTick(() => {})
      // 深度拷贝 移除VUE的监听，防止INDEX页面值变动
      this.record = JSON.parse(JSON.stringify(record))
      const placestatusOption = this.$options
      this.placestatusData = placestatusOption.filters['dictData']('place_status')
      const islockOption = this.$options
      this.islockData = islockOption.filters['dictData']('yes_or_no')
      const isfullOption = this.$options
      this.isfullData = isfullOption.filters['dictData']('yes_or_no')
      const hastaskdoingOption = this.$options
      this.hastaskdoingData = hastaskdoingOption.filters['dictData']('yes_or_no')
      const heightlevelOption = this.$options
      this.heightlevelData = heightlevelOption.filters['dictData']('height_level')
      this.$nextTick(() => {
        this.form.setFieldsValue({
          id: record.id,
          placecode: record.placecode,
          areaid: record.areaid,
          rowno: record.rowno,
          columnno: record.columnno,
          layerno: record.layerno,
          deepcellno: record.deepcellno,
          aisle: record.aisle,
          line: record.line,
          positionnoForSrm: record.positionnoForSrm,
          xzb: record.xzb,
          yzb: record.yzb,
          zzb: record.zzb,
          length: record.length,
          width: record.width,
          height: record.height,
          maxweight: record.maxweight,
          heightlevel: record.heightlevel + '',
          priority: record.priority
        })
      })
      this.WmsPlaceFkWmsAreaList()
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (values[key] == null) continue
            if (typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
              this.record[key] = values[key]
            } else {
              this.record[key] = values[key]
            }
          }
          WmsPlaceEdit(this.record)
            .then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
